<?php

namespace App\Http\Controllers;

use App\Http\Model\Parkingspace;
use App\Http\Model\Rent;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;

class RentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()//月租列表页显示
    {
        $json_string = file_get_contents('./config/carcity.json');
        $carcity = json_decode($json_string, true);//车牌城市引入
        //内连接查询
        $data = Rent::join('parkingspace','rent.parkingspace_no','parkingspace.id')//所有车位获取
            ->select('rent.*','parkingspace.status')
            ->get()
            ->toArray();

        return view('home/rent/index',compact('data','carcity'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()//月租办理页显示
    {
        $json_string = file_get_contents('./config/carcity.json');
        $carcity = json_decode($json_string, true);
        return view('home/rent/create',compact('carcity'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)//月租办理写入数据库方法
    {
        if($request->isMethod('post')){
            $input = Input::except('_token');
            $input['rentstart'] = date('Y-m-d',strtotime('now'));//月租开始时间
            $rentend = "+".$input['duration']."month";
            $input['rentend'] = date('Y-m-d',strtotime($rentend));//月租结束时间计算
            $admin = session('admin');
            $input['operator'] = $admin['name'];
            $input['order_id'] = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);//生成唯一订单号
            //写入月租表
            $rent_res = Rent::create($input)->toArray();

            //写入车位表
            $data = array(
                'plate_num' => $input['carcity'].'·'.$input['plate_num'],
                'rent_id' => $rent_res['id'],
                'is_rent' => '1',
                'status' => 0
            );

            $parkingspace_res= DB::table('parkingspace')//更新停车位信息
                ->where('id','=',$input['parkingspace_no'])
                ->update($data);

            if($rent_res&&$parkingspace_res){
                return response()->json(['code'=>1,'msg'=>'办理成功' ],200);
            }else{
                return response()->json(['code'=>0,'msg'=>'办理失败' ],200);
            }
        }else{
            return response()->json(['code'=>0,'msg'=>'请求方式不正确' ],200);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function rentsearch(){//异步搜索是月租车
        $input = Input::all();
        $carcity = $input['carcity'];
        $plate_num = $input['plate_num'];
        $data =  [];

        if($carcity&&!$plate_num){//根据车牌城市和车牌号查找
            $data = Rent::join('parkingspace','rent.parkingspace_no','parkingspace.id')
                ->where('rent.carcity','=',$carcity)
                ->where('parkingspace.status','=','0')
                ->select('rent.*','parkingspace.status')
                ->get()->toArray();
        }elseif(!$carcity&&$plate_num){
            $data = Rent::join('parkingspace','rent.parkingspace_no','parkingspace.id')
                ->where('rent.plate_num','like',$plate_num.'%')
                ->where('parkingspace.status','=','0')
                ->select('rent.*','parkingspace.status')
                ->get()->toArray();
        }elseif($carcity&&$plate_num){
            $data = Rent::join('parkingspace','rent.parkingspace_no','parkingspace.id')
                ->where('rent.carcity','=',$carcity)->where('rent.plate_num','like',$plate_num.'%')
                ->where('parkingspace.status','=','0')
                ->select('rent.*','parkingspace.status')
                ->get()->toArray();
        }
        if($data){
            return response()->json(['code'=>1,'msg'=>$data],200);
        }else{
            return response()->json(['code'=>0,'msg'=>'暂无数据'],200);
        }
    }

    //续租
    public function renewal($id){//续租显示
        $data = Rent::where('id','=',$id)->first()->toArray();
        return view('home/rent/renewal',compact('data'));
    }

    public function handlerenewal(){//续租操作
        $input = Input::all();
        $data = Rent::where('id','=',$input['id'])->first()->toArray();
        $str = "+".$input['duration']."month";
        $rentend = date('Y-m-d',strtotime($str));

        $res = DB::table('rent')->where('id','=',$input['id'])//更新月租表
            ->update(['rentend'=>$rentend,'duration'=>$input['duration']+$data['duration']]);
        if($res){
            return response()->json(['code'=>1,'msg'=>'操作成功'],200);
        }else{
            return response()->json(['code'=>0,'msg'=>'操作失败'],200);
        }
    }
}
